#※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※
# File Name: install_package.sh
# Author: GID5564
# Description: 安装软件包及依赖
# Created Time: 22/11/23-21:09:19
#※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※
#! /usr/bin/env bash

#更新pip包
function update_pip_package(){
    Msg "${BLUE}请输入要更新的Pip包名称: ${RESET}"
    read pkg
    if [[ ! -z ${pkg} ]];then
        pip install -i ${MODE_SOURCE} -U ${pkg}
    fi
}

#移除pip包
function remove_pip_package(){
    Msg "${BLUE}请输入要移除的pip包名称: ${RESET}"
    read pkg
    if [[ ! -z ${pkg} ]];then
        pip uninstall ${pkg}
    fi
}

#安装配置文件ini中软件包
function install_ini_dpkgs(){
    [[ -z "$1" ]] && {
        error "Usage: install_ini_dpkgs <key-name>"
        return $INVALID_ARGUMENTS
    }

    local pkgs=$(read_ini ${FILE_CONFIG} "apt" $1)
    msg "${GREEN}即将安装: ${RESET}[${RB_YELLOW}${pkgs}${RESET}]"
    press
    install_dpkg_packages ${pkgs}
    unset pkgs
}

function install_ini_ppkgs(){
    [[ -z "$1" ]] && {
        error "Usage: install_ini_ppkgs <key-name>"
        return $INVALID_ARGUMENTS
    }

    local pkgs=$(read_ini ${FILE_CONFIG} "pip" $1)
    msg "${GREEN}即将安装: ${RESET}[${RB_YELLOW}${pkgs}${RESET}]"
    press
    install_python_packages ${pkgs}
    unset pkgs
}


#安装配置文件ini中python项目所需软件包
function install_project(){
    [[ -z "$1" ]] && {
        error "Usage: install_project <key-name>"
        return $INVALID_ARGUMENTS
    }

    #value格式: 
    #    [apt包,…]@[pip包,…]/[项目标题]
    local value=$(read_ini ${FILE_CONFIG} "item" $1)
    local pkgs=${value///*}
    local title=${value##*/}
    local ise=$(IsEmpty ${pkgs})
    if [[ ${ise} -eq 1 ]];then
        local apts=${pkgs//@*}
        local pips=${pkgs##*@}
        if [[ -n ${apts} ]];then
            msg "${GREEN}即将安装: ${RESET}[${RB_YELLOW}${apts}${RESET}]"
            install_dpkg_packages ${apts}
        fi
        if [[ -n ${pips} ]];then
            msg "${GREEN}即将安装: ${RESET}[${RB_YELLOW}${pips}${RESET}]"
            install_python_packages ${pips}
        fi
        success "完成Python项目${title}相关软件包的安装!"
        unset apts
        unset pips
    fi
    unset ise
    unset title
    unset pkgs
    unset value
}

#安装项目软件包到虚拟环境中
function install_project_to_evn(){
    [[ -z "$1" ]] && {
        error "Usage: install_project_to_evn <evn-name>"
        return $INVALID_ARGUMENTS
    }

    local ENV_NAME=$1
    local NEW_ENV=""
    
    local ise=$(read_ini ${FILE_CONFIG} "env-enable" "installed")
    if [[ ${ise} = "false" ]];then
        #虚拟环境未安装
        warn "请先安装虚拟环境相关依赖如: ${RESET}${RED}virtualenv,virtualenvwrapper${RESET}"
        return 1
    fi
    unset ise
    #有虚拟环境则进入，否则新建
    if [[ $ENV_SUM -ne 0 ]];then
        for ((i=0;i<$ENV_SUM;i++))
        do
            if [[ "${ARRAY_ENVS[$i]}" == "${ENV_NAME}" ]];then
                NEW_ENV=${ENV_NAME}
                break
            fi
        done
    else
        NEW_ENV=${ENV_NAME}
        mkvirtualenv -p /usr/bin/python3 ${ENV_NAME} 
        #刷新数组
        virtualenv_str_to_array
    fi
    workon $SELECT_ENV 2>/dev/null
    local ret=$?
    [[ ${ret} -eq 0 ]] && {
        CUR_ENV=${NEW_ENV}
        PS1="(${PURPLE}$CUR_ENV${RESET})${_OLD_ENV_PS}"
        success "成功打开虚拟环境: $CUR_ENV"
    } || {
        error "出现未知错误,虚拟环境未打开!"
        unset ENV_NAME
        unset NEW_ENV
        return 1
    }
    unset ret
    msg "${BLUE}即将在虚拟环境[${RESET}${YELLOW}${NEW_ENV}${RESET}${BLUE}]中安装相关依赖包。${RESET}"
    press
    install_project ${ENV_NAME}
    unset ENV_NAME
    unset NEW_ENV
}

#虚拟环境中安装pip包
function install_ppkg_to_env(){
    [[ -z "$1" ]] && {
        error "Usage: install_ppkg_to_env <evn-name>"
        return $INVALID_ARGUMENTS
    }
    local ENV_NAME=$1
    local ise=$(read_ini ${FILE_CONFIG} "env-enable" "installed")
    if [[ ${ise} == "false" ]];then
        #虚拟环境未安装
        warn "请先安装虚拟环境相关依赖如: ${RESET}${RED}virtualenv,virtualenvwrapper${RESET}"

        return 1
    fi
    unset ise
    local pkgs=$(read_ini ${FILE_CONFIG} "pip" ${ENV_NAME})
    local NEW_ENV=""
    #有虚拟环境则进入，否则新建
    if [[ $ENV_SUM -ne 0 ]];then
        for ((i=0;i<$ENV_SUM;i++))
        do
            if [[ "${ARRAY_ENVS[$i]}" = "${ENV_NAME}" ]];then
                NEW_ENV=${ENV_NAME}
                break
            fi
        done
    else
        NEW_ENV=${ENV_NAME}
        mkvirtualenv -p /usr/bin/python3 ${ENV_NAME}
        #刷新数组
        virtualenv_str_to_array
    fi

    workon $SELECT_ENV 2>/dev/null
    local ret=$?
    [[ ${ret} -eq 0 ]] && {
        CUR_ENV=${NEW_ENV}
        PS1="(${PURPLE}$CUR_ENV${RESET})${_OLD_ENV_PS}"
        success "成功打开虚拟环境: $CUR_ENV"
    } || {
        error "出现未知错误,虚拟环境未打开!"
        unset ENV_NAME
        unset NEW_ENV
        unset pkgs
        return 1
    }
    unset ret
    msg "${BLUE}即将在虚拟环境[${RESET}${YELLOW}${NEW_ENV}${RESET}${BLUE}]中安装pip包: [${RESET}${RED}${pkgs}${RESET}${BLUE}]${RESET}"
    press
    install_python_packages ${pkgs}
    unset pkgs
    unset ENV_NAME
    unset NEW_ENV
}
































